/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testklient;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import theserver.DeviceInfo;
import theserver.DeviceList;

/**
 *
 * @author Emma
 */
class Controller implements Runnable {

    private String host;
    private int port;
    private ObjectOutputStream objectOutputStream;
    private ObjectInputStream objectInputStream;
    private Socket socket;
    private Thread thread = new Thread(this);
    private Object inputStreamObject;
    private boolean threadRun;
    private ServerSocketen ser;
    private DeviceInfo devI;
    private GUI gui;
    public Controller(String host, int port, DeviceInfo devI) {
        try {
            this.host = host;
            this.port = port;
            this.devI = devI;
            System.err.println("innan socket "+port+host);
            gui = new GUI();
            gui.setVisible(true);
            ser = new ServerSocketen(devI, gui, devI.getPort());
            socket = new Socket(host, port);
            System.err.println("efter socket    ");
            
            thread.start();
            threadRun = true;
            System.err.println("Klienten uppe");
            sendMessage(createDeviceInfo());


        } catch (UnknownHostException ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void sendMessage(Object o) {
        try {
           objectOutputStream = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
            objectOutputStream.writeObject(o);
            objectOutputStream.flush();
            System.err.println("Meddelandet skickat");
        } catch (IOException ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public DeviceInfo createDeviceInfo() {
      
        //DeviceInfo dev = new DeviceInfo("3", "Emma", "KTH-OPEN", "0707562384", "130.237.5.67", 55000);
        
        return devI;

    }

    public void run() {
        try {
            while(true){
            objectInputStream = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
            while (((inputStreamObject = objectInputStream.readObject()) != null) && threadRun) {
                if (inputStreamObject instanceof DeviceInfo) {
                    DeviceInfo dev = (DeviceInfo) inputStreamObject;
                    System.err.println(dev);
                } else if (inputStreamObject instanceof ArrayList) {
                    ArrayList<DeviceInfo> lists = (ArrayList<DeviceInfo>) inputStreamObject;
                    for (int i = 0; i < lists.size(); i++) {
                        System.err.println(lists.get(i));

                    }
                }
              
                
                else if(inputStreamObject instanceof Integer){
                    Integer i = (Integer) inputStreamObject;
                    System.err.println("logginin int: "+ i);

                }
            }}
        } catch (IOException ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
